arrayN = [{
	id: 1,
	distance: 1
}, {
	id: 2,
	distance: 2
}, {
	id: 3,
	distance: 3
}, {
	id: 4,
	distance: 4
}];

arrayDistanceBetweenN = [{
	beginId: 1,
	endId: 2,
	distance: 10
}, {
	beginId: 1,
	endId: 3,
	distance: 2
}, {
	beginId: 1,
	endId: 4,
	distance: 3
}, {
	beginId: 2,
	endId: 3,
	distance: 4
}, {
	beginId: 1,
	endId: 4,
	distance: 5
}, {
	beginId: 3,
	endId: 4,
	distance: 18
}];

deliveryMan = [4,4,6];

//OrderDeliveryC(arrayN,arrayDistanceBetweenN,deliveryMan,4);

/**
 * 获取距离最大的两个点
 */
function getMaxDistance(arrayDistanceBetweenN) {

	var arrayDBNlng = arrayDistanceBetweenN.length;
	var result = arrayDistanceBetweenN[0];

	for (var i = 1; i < arrayDBNlng; i++) {
		var currDisBtwn = arrayDistanceBetweenN[i];
		if (currDisBtwn.distance > result.distance || (currDisBtwn.distance == result.distance && (currDisBtwn.beginId + beginId.endId < result.beginId + result.endId))) {
			result = currDisBtwn;
		}
	}

	return result;

}

/**
 * 获取距离最近的点
 */
function getMinDistance(arrayDistanceBetweenN, id) {

	var arrayDBNlng = arrayDistanceBetweenN.length;
	var minDistance = null;
	var result = {};
	result.compareId = 0;

	for (var i = 0; i < arrayDBNlng; i++) {
		var currDisBtwn = arrayDistanceBetweenN[i];
		if ((currDisBtwn.beginId == id || currDisBtwn.endId == id) && (minDistance == null || (currDisBtwn.distance < minDistance.distance))) {
			minDistance = currDisBtwn;
			result.beginId = currDisBtwn.beginId;
			result.endId = currDisBtwn.endId;
			if(currDisBtwn.beginId == id) {
				result.compareId = currDisBtwn.endId;
			} else {
				result.compareId = currDisBtwn.beginId;
			}
			
		}
	}

	result.minDistance = minDistance;
	return result;

}

/**
 * 根据已经分好的块 获取距离最近的点
 */
function getMinDistanceWithResult(arrayDistanceBetweenN, result, id) {

	var minLength;
	var minBlockId; //最近距离分到哪个块
	for(var i = 0; i < result.length; i++) {
		var currLength = getTwoPointDistance(arrayDistanceBetweenN, result[i][result[i].length - 1].id, id);
		if(!minLength || currLength < minLength) {
			minLength = currLength;
			minBlockId = i;
		}
	}
	return minBlockId;
}

/**
 * 根据 beginId, endId 从arrayDistanceBetweenN 中 删除元素
 */
function deleteOrder(arrayDistanceBetweenN, beginId, endId) {
	for (var i = 0; i < arrayDistanceBetweenN.length; i++) {
		var currDisBtwn = arrayDistanceBetweenN[i];
		if ((currDisBtwn.beginId == beginId && currDisBtwn.endId == endId)) {
			arrayDistanceBetweenN.splice(i, 1);
			i--;
		}
	}
}

function deleteArrayN(arrayN, id) {
	for (var i = 0; i < arrayN.length; i++) {
		if (arrayN[i].id == id ) {
			arrayN.splice(i, 1);
			i--;
		}
	}
}

/**
 * 测试
 */
function deleteOrderTest(arrayDistanceBetweenN) {
	deleteOrder(arrayDistanceBetweenN, 1, 2);
	console.log(arrayDistanceBetweenN);
}

/**
 * 计算两点间距离
 */
function getTwoPointDistance(arrayDistanceBetweenN, currId, id) {
	for(var i=0; i < arrayDistanceBetweenN.length; i++ ) {
		var currDB = arrayDistanceBetweenN[i];
		if((currDB.beginId == currId && currDB.endId == id) || (currDB.beginId == id && currDB.endId == currId) ) {
			return currDB.distance;
		}
	}
	return 0;
}

/**
 * 算法C 如果只有1个外送员 或者 只有一笔订单 无须计算 请不要调用本算法
 * 确保外送员足够 否则 要在调用算法C之前提示 增加外送员
 * deliveryMan = [4,6]; // 数组长度 外送员人数  数组值 最大订单数 也就是 orderLimit
 */
function OrderDeliveryC(arrayN, arrayDistanceBetweenN, deliveryMan, maxDeliveryLength) {

	var arrayDBNlng = arrayDistanceBetweenN.length;
	var deliveryManLng = deliveryMan.length;
	var result = new Array();


	var maxDistanceOrder = getMaxDistance(arrayDistanceBetweenN);

	console.log("******");
	console.log(maxDistanceOrder);


	// 把距离最大的两个点 加入分组
	for (var i = 0; i < arrayN.length; i++) {
		var currArrayDBNlng = arrayN[i];
		if (currArrayDBNlng.id == maxDistanceOrder.beginId) {
			result[0] = new Array();
			result[0].push(arrayN[i]);
			deleteArrayN(arrayN,arrayN[i].id);
			i--;
			deleteOrder(arrayDistanceBetweenN, maxDistanceOrder.beginId, maxDistanceOrder.endId);
		} 

		if (currArrayDBNlng.id == maxDistanceOrder.endId) {
			result[1] = new Array();
			result[1].push(arrayN[i]);
			deleteArrayN(arrayN,arrayN[i].id);
			i--;
			deleteOrder(arrayDistanceBetweenN, maxDistanceOrder.beginId, maxDistanceOrder.endId);
		}
	}

	/*console.log(result);
	console.log(result.length);
	console.log(result[0].length);
	console.log(result[1].length);*/

	// 两个送货员
	if (deliveryManLng == 2) {

		while (arrayDistanceBetweenN.length > 0) {
			var minDistanceResult1 = getMinDistance(arrayDistanceBetweenN, result[0][0].id);
			var minDistanceResult2 = getMinDistance(arrayDistanceBetweenN, result[1][0].id);

			if (minDistanceResult1.compareId != minDistanceResult2.compareId) {
				if (result[0].length < deliveryMan[0]) {
					result[0].push(arrayDBNlng[minDistanceResult1.compareId]);
					deleteOrder(arrayDistanceBetweenN, minDistanceResult1.beginId, minDistanceResult1.endId);
				} else {
					result[1].push(arrayDBNlng[minDistanceResult1.compareId]);
					deleteOrder(arrayDistanceBetweenN, minDistanceResult1.beginId, minDistanceResult1.endId);
				}

				if (result[1].length < deliveryMan[1]) {
					result[1].push(arrayDBNlng[minDistanceResult2.compareId]);
					deleteOrder(arrayDistanceBetweenN, minDistanceResult2.beginId, minDistanceResult2.endId);
				} else {
					result[0].push(arrayDBNlng[minDistanceResult2.compareId]);
					deleteOrder(arrayDistanceBetweenN, minDistanceResult2.beginId, minDistanceResult2.endId);
				}
			} else { //if(minDistanceResult1.compareId == minDistanceResult2.compareId) 
				if (minDistanceResult1.minDistance < minDistanceResult2.minDistance) {
					if (result[0].length < deliveryMan[0]) {
						result[0].push(arrayDBNlng[minDistanceResult1.compareId]);
						deleteOrder(arrayDistanceBetweenN, minDistanceResult1.beginId, minDistanceResult1.endId);
					} else {
						result[1].push(arrayDBNlng[minDistanceResult1.compareId]);
						deleteOrder(arrayDistanceBetweenN, minDistanceResult1.beginId, minDistanceResult1.endId);
					}
				} else {
					if (result[1].length < deliveryMan[1]) {
						result[1].push(arrayDBNlng[minDistanceResult2.compareId]);
						deleteOrder(arrayDistanceBetweenN, minDistanceResult2.beginId, minDistanceResult2.endId);
					} else {
						result[0].push(arrayDBNlng[minDistanceResult2.compareId]);
						deleteOrder(arrayDistanceBetweenN, minDistanceResult2.beginId, minDistanceResult2.endId);
					}
				}
			}
		}

	} else {

		console.log("--------------");
		console.log(arrayN);
		console.log("--------------");

		while(result.length < deliveryManLng){ //有多少外送员 就要分多少组
			var maxMatch = 0;
			var maxMatchOrders = arrayN; // 匹配值最大的若干笔订单
			for (var i = 0; i < arrayN.length; i++) {
				var currDisBtwn = arrayN[i];
				currDisBtwn.match = 0;
				// 与距离最大两个点 相关的点
				//if (currDisBtwn.beginId == maxDistanceOrder.beginId || currDisBtwn.endId == maxDistanceOrder.beginId || currDisBtwn.beginId == maxDistanceOrder.endId || currDisBtwn.endId == maxDistanceOrder.endId) {
				if (currDisBtwn.id == maxDistanceOrder.beginId || currDisBtwn.id == maxDistanceOrder.endId) {
					if (currDisBtwn.distance > maxDeliveryLength / 2) {
						currDisBtwn.match++;
						if (currDisBtwn.match > maxMatch) {
							maxMatch = currDisBtwn.match;
							maxMatchOrders = [];
							console.log(currDisBtwn);
							maxMatchOrders.push(currDisBtwn);
						} else if (currDisBtwn.match == maxMatch) {
							console.log(currDisBtwn);
							maxMatchOrders.push(currDisBtwn);
						}
					}
				}
			}

			//console.log(maxMatchOrders);
			//console.log(result);

			var maxMatchOneOrder;
			var maxMatchOneOrderLength;
			for(var i = 0; i < maxMatchOrders.length; i++) {
				var currOrderlength = 0;
				var currMaxMatch = maxMatchOrders[i];
				for(var j = 0; j < result.length; j++) {
					//计算距离和 分块时 每个外送员只有一笔单 result[i][0]
					//currOrderlength += getTwoPointDistance(arrayDistanceBetweenN, currMaxMatch.beginId, currMaxMatch.endId, result[j][0].id);
					currOrderlength += getTwoPointDistance(arrayDistanceBetweenN, currMaxMatch.id, result[j][0].id);
				}

				if(!maxMatchOneOrder || (maxMatchOneOrderLength < currOrderlength)){
					maxMatchOneOrderLength = currOrderlength; // 距离最大的点
					maxMatchOneOrder = currMaxMatch;
				}
			}

			if(maxMatchOneOrder) {
				var idx = result.length;
				result[idx] = new Array();
				delete maxMatchOneOrder.match;
				result[idx].push(maxMatchOneOrder);
				deleteArrayN(arrayN, maxMatchOneOrder.id);
			} else {
				console.log('Error');
				return;
			}
		}
		
		console.log(result);

		// 分块完成 根据2个送货员的情况 将其他订单放入不同分组
		for (var i = 0; i < arrayN.length; i++) {
			var idx = getMinDistanceWithResult(arrayDistanceBetweenN, result, arrayN[i].id);
			delete arrayN[i].match;
			result[idx].push(arrayN[i]);
			deleteArrayN(arrayN, arrayN[i].id);
			i--;
		}
		
	}


	console.log("result:");
	console.log(result);
}